package cn.itcast.order.web;

import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId, @RequestHeader(value = "name",required = false) String name) {
        System.out.println(name);
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        // 根据关联的userId 远程查询用户信息
//        User user = restTemplate.getForObject("http://userservice/user/" + order.getUserId(), User.class);
        User user = userClient.queryById(order.getUserId());
        // 将用户信息设置到订单中
        order.setUser(user);
        return order;
    }
}
